/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package appletthreatr;

//import java.awt.Color;
//import java.awt.Font;
//import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 *
 * @author julita
 */
public class JAppletForm1 extends java.applet.Applet implements Runnable {

    /**
     * Initializes the applet JAppletForm1
     */
    @Override
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    initComponents();
                    horaAct = sHoraAct();
                    parado = true;
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void start() {
        /*
         * solo si se desea que arranque desde el principio se pondría este if
         */
//        if (hilo == null) {
//            hilo = new Thread(this);
//            hilo.start();
//            parado = false;
//        }
    }

    @Override
    public void stop() {
        hilo = null;
    }

    @Override
    public void run() {
        Thread hiloEnProceso = Thread.currentThread();
        while (hilo == hiloEnProceso && !parado) {
            horaAct = sHoraAct();
            jLabel1.setText(horaAct);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
            }
        }
    }

    private String sHoraAct() {
        return new SimpleDateFormat("HH:MM:ss").format(Calendar.getInstance().getTime());
        //desgloasdo sería:
//    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
//    Calendar cal = Calendar.getInstance();
//    horaAct = sdf.format(cal.getTime());
//    return horaAct;
    }

    /**
     * This method is called from within the init() method to initialize the
     * form. WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton1.setText("IniciarCont");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, -1, 50));

        jButton2.setText("Continuar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 20, -1, 50));

        jButton3.setText("PararCont");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 20, -1, 50));

        jLabel1.setText("jLabel1");
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 124, 200, 20));
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        parado = true;
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (hilo == null || !hilo.isAlive()) {
            hilo = new Thread(this);
            hilo.start();
            parado = false;
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
          if (hilo == null) {
            hilo = new Thread(this);
            hilo.start();
            parado = false;
            jButton1.setEnabled(false);
        }
    }//GEN-LAST:event_jButton1ActionPerformed
    private Thread hilo;
    private String horaAct;
    boolean parado;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}
